/*
 * @Author: Robin
 * @Date: 2020/6/29 18:01
 * @LastEditors: Robin
 * @LastEditTime: 2020/6/29 18:01
 * @Description: 发送邮件
 */
package main

import (
	"fmt"
	"net/smtp"
	"strings"
	"time"
)

func main() {
	fmt.Println(time.Now())
	err := SendToMail("email address", "password", "smtp.sina.com", "25", "email1,email2", "测试主题", "<html><body><h1>Hello Robin</h1></body></html>", "html")
	fmt.Println(err)
	fmt.Println(time.Now())
}

// 发送邮件
func SendToMail(user, password, host, port, to, subject, body, mailType string) error {
	auth := smtp.PlainAuth("", user, password, host)
	var contentType string
	if mailType == "html" {
		contentType = "Content-Type: text/" + mailType + "; charset=UTF-8"
	} else {
		contentType = "Content-Type: text/plain" + "; charset=UTF-8"
	}

	msg := []byte("To: " + to + "\r\nFrom: " + user + "\r\nSubject: " + subject + "\r\n" + contentType + "\r\n\r\n" + body)
	sendTo := strings.Split(to, ";")
	err := smtp.SendMail(host+":"+port, auth, user, sendTo, msg)
	return err
}
